package com.snap.vseries.chart;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

import com.github.mikephil.charting.animation.ChartAnimator;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.interfaces.dataprovider.LineDataProvider;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.github.mikephil.charting.renderer.LineChartRenderer;
import com.github.mikephil.charting.utils.Transformer;
import com.github.mikephil.charting.utils.ViewPortHandler;
import com.snap.vseries.Constants;

import java.util.List;

public class PointLineChartRenderer extends LineChartRenderer {

    private float[] mCirclesBuffer = new float[2];
    private Paint mPaint;

    public PointLineChartRenderer(LineDataProvider chart, ChartAnimator animator, ViewPortHandler viewPortHandler) {
        super(chart, animator, viewPortHandler);
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(2);
    }

    @Override
    public void drawExtras(Canvas c) {
        super.drawExtras(c);
        drawExtraPoint(c);
    }

    private void drawExtraPoint(Canvas c) {

        float phaseY = mAnimator.getPhaseY();

        mCirclesBuffer[0] = 0;
        mCirclesBuffer[1] = 0;

        List<ILineDataSet> dataSets = mChart.getLineData().getDataSets();

        for (int i = 0; i < dataSets.size(); i++) {

            if (dataSets.get(i) instanceof PointLineDataSet) {

                PointLineDataSet dataSet = (PointLineDataSet) dataSets.get(i);
                if (dataSet.getExtraPoint().size() == 0) continue;
                if (!dataSet.isVisible()) continue;
                Transformer trans = mChart.getTransformer(dataSet.getAxisDependency());

                float low = mChart.getLowestVisibleX();
                float high = mChart.getHighestVisibleX();
                List<Entry> entries = dataSet.getExtraPoint();
                for (int j = 0; j < entries.size(); j++) {
                    Entry e = entries.get(j);
                    if (e.getX() < low || e.getX() > high) continue;
                    mCirclesBuffer[0] = e.getX();
                    mCirclesBuffer[1] = e.getY() * phaseY;

                    trans.pointValuesToPixel(mCirclesBuffer);

                    if (!mViewPortHandler.isInBoundsRight(mCirclesBuffer[0]))
                        continue;

                    if (!mViewPortHandler.isInBoundsLeft(mCirclesBuffer[0]) ||
                            !mViewPortHandler.isInBoundsY(mCirclesBuffer[1]))
                        continue;

                    if (j == 0) {
                        int channelIndex = (int) e.getData();
                        mPaint.setColor(Constants.CHANNEL_DEFAULT_COLORS[channelIndex]);
                        c.drawRect(mCirclesBuffer[0] - 2, mCirclesBuffer[1] - 2, mCirclesBuffer[0] + 2, mCirclesBuffer[1] + 2, mPaint);
                        mPaint.setColor(Color.WHITE);
                        c.drawPoint(mCirclesBuffer[0], mCirclesBuffer[1], mPaint);
                    } else {
                        mPaint.setColor(0xff3a3a3a);
                        c.drawRect(mCirclesBuffer[0] - 0.5f, mCirclesBuffer[1] - 1.5f, mCirclesBuffer[0] + 0.5f, mCirclesBuffer[1] + 1.5f, mPaint);
                    }
                }
            }
        }
    }
}